# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=glib2
pkgname=(glib2 glib2-devel glib2-docs)
pkgver=2.44.1
pkgrel=1
pkgdesc="Common C routines used by GTK+ and other libs"
url="http://www.gtk.org/"
arch=('i686' 'x86_64')
makedepends=('pkg-config' 'python' 'libxslt-devel' 'docbook-xsl' 'pcre-devel' 'libffi-devel' 'libiconv-devel' 'gamin-devel' 'gtk-doc' 'zlib-devel')
source=(http://ftp.gnome.org/pub/GNOME/sources/glib/${pkgver%.*}/glib-$pkgver.tar.xz
        revert-warn-glib-compile-schemas.patch
        2.16-tests.patch
        2.24.1-gmodule.patch
        2.34.3-m4macros.patch
        2.36.3-gcredentials-cygwin.patch
        2.36.3-not-win32.patch
        2.36.3-visibility.patch
        2.36.4-gmutex-cygwin.patch
        2.36.4-goption-cygwin.patch
        2.36.4-gspawn-cygwin.patch
        2.36.4-gtk-doc-vpath.patch
        2.36.4-gutf8-cygwin.patch
        glib-2.38.1-msys2.patch)
sha256sums=('8811deacaf8a503d0a9b701777ea079ca6a4277be10e3d730d2112735d5eca07'
            '049240975cd2f1c88fbe7deb28af14d4ec7d2640495f7ca8980d873bb710cc97'
            'f3f5ab46126132d9d666fea410f599f103a1d1847eba8fc77871ebc48b4bddbb'
            'bc1d90c66e4eed39fe4c5720c966e3dd44451df1de676c028cca6f7b06dcfd3a'
            '9095183f944334e2d02ca67b363d43ea7508c2b02c6e91905160786e7bccbaa7'
            '2c6a6dd89501ec4cf653ad19c2f751de56b62b2228f8729578d2afda9dc02d33'
            '6a0dd9bb6cddf8a6b811a3509582f46ad15bbbfa15150bee85aed74b5ef57f76'
            '23a544c39a8596dfcd59b0226a3228d04c288c2023f6069942c3a76b8397e93b'
            'affc358cab65230e0722d9c541ae52896f1f63cdabd2379bceb28a92c9a6ea68'
            '64397f64fd60ab48a28f05b8cf29e1eb6f95adf384b81de1ac8e74e51385a3de'
            '5a5df4d7ca0ae2be859a80731041fd9cfe6e1a338a882702d1e7d820816d8866'
            '8d5e710b57be0527c34afb0894fefe9ca8aec1007e424a303a15889985227e05'
            '5016c0af3a21a03fc1a979a5de21f2116edc75c88f2980e0f2ab816eff98680b'
            '8ba895115423c5ab3079c48e610fa9f1387e5717dcbb06f41f8f6b1730d219a6')

prepare() {
  cd glib-$pkgver
  patch -Rp1 -i ../revert-warn-glib-compile-schemas.patch

  patch -p2 -i ${srcdir}/2.16-tests.patch
  patch -p2 -i ${srcdir}/2.24.1-gmodule.patch
  patch -p2 -i ${srcdir}/2.34.3-m4macros.patch
  #patch -p2 -i ${srcdir}/2.36.3-gcredentials-cygwin.patch
  patch -p2 -i ${srcdir}/2.36.3-not-win32.patch
  patch -p2 -i ${srcdir}/2.36.3-visibility.patch
  #patch -p2 -i ${srcdir}/2.36.4-gmutex-cygwin.patch
  patch -p2 -i ${srcdir}/2.36.4-goption-cygwin.patch
  patch -p2 -i ${srcdir}/2.36.4-gspawn-cygwin.patch
  patch -p2 -i ${srcdir}/2.36.4-gtk-doc-vpath.patch
  patch -p2 -i ${srcdir}/2.36.4-gutf8-cygwin.patch
  patch -p1 -i ${srcdir}/glib-2.38.1-msys2.patch

  libtoolize --force --copy
  autoreconf -fi
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  cd glib-$pkgver
  ./configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --prefix=/usr \
    --disable-libelf \
    --with-pcre=system \
    --with-threads=posix \
    --enable-shared \
    --disable-static \
    --disable-silent-rules \
    ac_cv_header_winsock2_h=no \
    ac_cv_header_mswsock_h=no
  make VERBOSE=1

  make completiondir=/usr/share/bash-completion/completions DESTDIR="$srcdir/dest" install
  pushd $srcdir/dest/usr/share/gdb/auto-load > /dev/null
  mv libglib* msys-glib-2.0-0.dll-gdb.py
  mv libgobject* msys-gobject-2.0-0.dll-gdb.py
  popd > /dev/null
}

check() {
  cd glib-$pkgver
  #make -k check || :
}

package_glib2() {
  depends=('libxslt' 'libpcre' 'libffi' 'libiconv' 'zlib')
  optdepends=('gamin: for gio fam module'
              'python2: for gdbus-codegen and gtester-report')
  options=('!docs' '!emptydirs')
  license=('LGPL')

  #cd glib-$pkgver
  #make completiondir=/usr/share/bash-completion/completions DESTDIR="$pkgdir" install

  mkdir -p $pkgdir/usr/{bin,lib,share}
  cp -f $srcdir/dest/usr/bin/*.dll $pkgdir/usr/bin/
  for file in gapplication gdbus gio-querymodules glib-compile-schemas gobject-query gsettings; do
    cp -f $srcdir/dest/usr/bin/${file}.exe $pkgdir/usr/bin/
  done

  cp -rf $srcdir/dest/usr/lib/gio $pkgdir/usr/lib/
  #cp -rf $srcdir/dest/usr/share/doc $pkgdir/usr/share/
  cp -rf $srcdir/dest/usr/share/gdb $pkgdir/usr/share/

  mkdir -p $pkgdir/usr/share/{man,glib-2.0}
  cp -rf $srcdir/dest/usr/share/glib-2.0/gdb $pkgdir/usr/share/glib-2.0/
  cp -rf $srcdir/dest/usr/share/glib-2.0/schemas $pkgdir/usr/share/glib-2.0/
  cp -rf $srcdir/dest/usr/share/locale $pkgdir/usr/share/
  #mkdir -p $pkgdir/usr/share/man/man1
  #cp -rf $srcdir/dest/usr/share/man/man1/gdbus.* $pkgdir/usr/share/man/man1/
  #cp -rf $srcdir/dest/usr/share/man/man1/gio* $pkgdir/usr/share/man/man1/
  #cp -rf $srcdir/dest/usr/share/man/man1/glib-compile-schemas* $pkgdir/usr/share/man/man1/
  #cp -rf $srcdir/dest/usr/share/man/man1/gobject* $pkgdir/usr/share/man/man1/
  #cp -rf $srcdir/dest/usr/share/man/man1/gsettings.* $pkgdir/usr/share/man/man1/

  cp -rf $srcdir/dest/usr/share/bash-completion $pkgdir/usr/share/

  for _i in "$pkgdir/usr/share/bash-completion/completions/"*; do
    chmod -x "$_i"
  done
}

package_glib2-devel() {
  pkgdesc="glib2 headers and libraries"
  groups=('development')
  depends=("glib2=${pkgver}" 'pcre-devel' 'libffi-devel' 'libiconv-devel' 'zlib-devel')
  options=('emptydirs')

  mkdir -p $pkgdir/usr/{bin,lib,share}
  cp -f $srcdir/dest/usr/bin/gdbus-codegen $pkgdir/usr/bin/
  cp -f $srcdir/dest/usr/bin/glib-* $pkgdir/usr/bin/
  rm -f $pkgdir/usr/bin/glib-compile-schemas*
  cp -f $srcdir/dest/usr/bin/gresource* $pkgdir/usr/bin/
  cp -f $srcdir/dest/usr/bin/gtester* $pkgdir/usr/bin/

  cp -rf $srcdir/dest/usr/lib/pkgconfig $pkgdir/usr/lib/
  cp -f $srcdir/dest/usr/lib/*.a $pkgdir/usr/lib/
  #cp -rf $srcdir/dest/usr/lib/gio* $pkgdir/usr/lib/
  cp -rf $srcdir/dest/usr/lib/glib* $pkgdir/usr/lib/

  cp -rf $srcdir/dest/usr/include $pkgdir/usr/
  cp -rf $srcdir/dest/usr/share/aclocal $pkgdir/usr/share/
  cp -rf $srcdir/dest/usr/share/glib-2.0 $pkgdir/usr/share/
  rm -rf $pkgdir/usr/share/glib-2.0/gdb
  rm -rf $pkgdir/usr/share/glib-2.0/schemas

  #mkdir -p $pkgdir/usr/share/man/man1
  #cp -rf $srcdir/dest/usr/share/man/man1/gdbus-codegen* $pkgdir/usr/share/man/man1/
  #cp -rf $srcdir/dest/usr/share/man/man1/glib-* $pkgdir/usr/share/man/man1/
  #rm -f $pkgdir/usr/share/man/man1/glib-compile-schemas*
  #cp -rf $srcdir/dest/usr/share/man/man1/gresource* $pkgdir/usr/share/man/man1/
  #cp -rf $srcdir/dest/usr/share/man/man1/gtester* $pkgdir/usr/share/man/man1/
}

package_glib2-docs() {
  pkgdesc="Documentation for glib2"
  conflicts=('gobject2-docs')
  replaces=('gobject2-docs')
  license=('custom')
  options=('docs' '!emptydirs')

  cd glib-$pkgver/docs
  make DESTDIR="${pkgdir}" install
  install -m755 -d "${pkgdir}/usr/share/licenses/glib2-docs"
  install -m644 reference/COPYING "${pkgdir}/usr/share/licenses/glib2-docs/"

  rm -rf "${pkgdir}/usr/share/man"
}
